<template>
  <div>
    <el-col :span="24" class="bg1">
      <div class="grid-content bg-purple">
        <FormPanel @formChange="changeHandle" ref="formPanel" :list="formList" :cols="3" :labelWidth="110" :isSubmitBtn="false" />
      </div>
    </el-col>

    <div class="dialogBottmView">
      <multiuse-button type="primary" class="btnStyle" v-if="handleType == 'add'" :click="closeMenu.bind(this, 1)">保存 </multiuse-button>

      <multiuse-button type="primary" class="btnStyle" :click="closeMenu.bind(this, 2)">关闭</multiuse-button>
    </div>
  </div>
</template>

<script>
import { addThreeAssembly, threeInfoList } from '@/api/repair/threePacksOfManager/assemblyAndRegulationsCDuan';
import { getAllUser } from '@/api/repair/batchEventManagement/activityDistribution';
import { arrayObjDeWeight } from '@/utils';
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { isEmpty } from '@/utils/utils';
import { exportExcelFile } from '@/utils/index';
import Cookies from 'js-cookie';
import { banWord } from '@/utils/validate.js';

export default {
  name: 'add',
  mixins: [dictionary],
  props: ['handleType', 'formData'],
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      detailObj: {},
      username: '',
      threeInfoList: []
    };
  },
  components: {},
  mounted() {
    this.initSelect();

    this.getThreeInfoList();

    if (this.handleType == 'show') {
      this.getData();
    }
  },
  methods: {
    /*获取法规列表*/
    async getThreeInfoList() {
      let res = await threeInfoList();
      if (res.resultCode == 200 && res.success == true) {
        let data = isEmpty(res['data']) ? [] : res['data'];
        this.threeInfoList = data;
        this.formList.find(item => item.fieldName == 'threeNo').itemList = data;
      }
    },

    async initSelect() {
      let res = await getAllUser();
      // 去重
      let dataList = arrayObjDeWeight(res.data, 'userId');

      this.formList.find(item => item.fieldName === 'createdBy').itemList = dataList.map(x => ({
        text: x.employeeName,
        value: x.userId
      }));
    },

    changeHandle(val) {
      this.saveData = val;
    },

    /*根据ID查询*/
    async getData() {
      let res = this;

      let data = isEmpty(this.formData) ? {} : this.formData;

      for (let i in data) {
        this.formList.map(x => {
          if (i == x.fieldName) {
            x.disabled = true;
            if (x.fieldName == 'createdBy') {
              x.initialValue = Number(data[i]);
            } else {
              x.initialValue = data[i];
            }
          }
        });
      }
    },
    async closeMenu(type) {
      if (type == 1) {
        /*保存数据*/
        this.$refs.formPanel.SUBMIT_FORM();

        if (this.saveData) {
          let obj = this.saveData;

          // if (this.handleType == 'add')
          // {
          //   obj.createdBy = Cookies.get('userId', "")
          // }

          let res = await addThreeAssembly(obj);
          if (res.resultCode == 200 && res.success == true) {
            this.$emit('close', 'refresh');
            this.$notify.success({ title: '提示信息', message: '操作成功' });
          } else {
            // let errMsg = isEmpty(res.errMsg) ? '操作失败' : res.errMsg;
            //
            // this.$notify.error({title: '提示信息', message: errMsg});
          }
        }
      } else {
        this.$emit('close');
      }
    },

    createFormList() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '总成与法规关系维护'
        },
        {
          type: 'SELECT',
          label: '规则编号',
          fieldName: 'threeNo',
          disabled: false,
          placeholder: '',
          rules: [{ required: true, trigger: 'change' }],
          initialValue: '',
          itemList: [],
          change: p => {
            let name = this.threeInfoList.find(item => item.value == p).name;
            name = isEmpty(name) ? '' : name;
            this.formList.find(item => item.fieldName == 'name').initialValue = name;
            console.log('p----', p);
          }
        },
        {
          type: 'INPUT',
          label: '规则名称',
          fieldName: 'name',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          placeholder: '请输入',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '总成代码',
          fieldName: 'assemblyCode',
          placeholder: '',
          disabled: false,
          rules: [
            { required: true, message: '总成代码不能为空', trigger: 'change' },
            { required: true, validator: banWord, trigger: 'change' }
          ],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '总成名称',
          fieldName: 'assemblyName',
          placeholder: '请选择',
          rules: [{ required: true, trigger: 'change', message: '总成名称不能为空' }],
          disabled: false,
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '创建人',
          itemList: [],
          fieldName: 'createdBy',
          placeholder: '请选择',
          disabled: true,
          initialValue: isEmpty(Cookies.get('userId', '')) ? 0 : Number(Cookies.get('userId', ''))
        },
        {
          type: 'DATE',
          label: '创建时间',
          fieldName: 'createdAt',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: moment().format('YYYY-MM-DD HH:mm:ss')
        },
        {
          type: 'TEXT_AREA',
          label: '总检备注',
          fieldName: 'remarks',
          placeholder: '请选择',
          disabled: false,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: '',
          selfCols: 3,
          maxlength: 300
        }
      ];
    }
  }
};
</script>

<style scoped lang="less">
.bg1 {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  padding-left: 0;
  padding-top: 15px;
  padding-bottom: 15px;
}

.bg2 {
  margin-top: 10px;
  margin-bottom: 10px;
}

.btn {
  text-align: center;
}

.btnStyle {
  margin-top: 10px;
}
</style>
